package com.demo.controller;

import com.demo.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/delete")
public class UserDeleteController extends HttpServlet {
    private ObjectMapper mapper = new ObjectMapper();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //转编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //跨域请求配置
        resp.setHeader("Access-Control-Allow-Origin", "*");

        //接收参数，这里是发过来删除的ID
        String id = req.getParameter("id");
        int userId = Integer.parseInt(id);
        //创建容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
         Boolean b = userService.del(userId);
         resp.getWriter().print(b?"删除成功":"删除失败");
    }
}
